package his.controller;

import com.alibaba.fastjson.JSONObject;
import his.entity.DiseCategory;
import his.entity.User;
import his.service.DiseCategoryService;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/diag/")
public class DiseCategoryController {

    @Autowired
    DiseCategoryService diseCategoryService;


    @GetMapping("all")
    @ResponseBody
    public CommonResponse<List<DiseCategory>> getall_disecategorys()
    {

        return  diseCategoryService.getall_disecategorys();
    }

    @PostMapping("add")
    @ResponseBody
    public CommonResponse<List<DiseCategory>> add_disecategory(HttpServletRequest httpServletRequest1)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest1);
        String dicaCode=(String) jsonObject.get("dicaCode");
        String dicaName=(String) jsonObject.get("dicaName");

        int sequenceNo=Integer.parseInt((String) jsonObject.get("sequenceNo"))  ;
        int dicaType=Integer.parseInt((String) jsonObject.get("dicaType"));
        return  diseCategoryService.add_disecategory(dicaCode,dicaName,sequenceNo,dicaType);
    }

    @PostMapping("modify/{sequence_no}")
    @ResponseBody
    public CommonResponse<List<DiseCategory>> update_disecategory(@PathVariable int sequence_no,HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String dicaCode=(String) jsonObject.get("dicaCode");
        String dicaName=(String) jsonObject.get("dicaName");
        int sequenceNo=(int) jsonObject.get("sequenceNo");
        int dicaType=(int) jsonObject.get("dicaType");
        return  diseCategoryService.update_disecategory(sequence_no,dicaCode,dicaName,sequenceNo,dicaType);
    }

    @DeleteMapping("del/{sequence_no}")
    @ResponseBody
    public CommonResponse<List<DiseCategory>> delete_disecategory(@PathVariable int sequence_no)
    {

        return  diseCategoryService.delete_disecategory(sequence_no);
    }

    @GetMapping("search/{keyword}")
    @ResponseBody
    public CommonResponse<List<DiseCategory>> getall_disecategory_bycodename(@PathVariable String keyword)
    {
        return  diseCategoryService.getall_disecategory_bycodename(keyword);
    }

}
